

<!DOCTYPE html>
<html class="writer-html5" lang="zh" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>例外情况 &mdash; Scrapy 2.3.0 文档</title>
  

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster.custom.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster.bundle.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-shadow.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-punk.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-noir.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-light.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-borderless.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/micromodal.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/sphinx_rtd_theme.css" type="text/css" />

  
  
  
  

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/language_data.js"></script>
        <script src="../_static/js/hoverxref.js"></script>
        <script src="../_static/js/tooltipster.bundle.min.js"></script>
        <script src="../_static/js/micromodal.min.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="登录" href="logging.html" />
    <link rel="prev" title="设置" href="settings.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home" alt="Documentation Home"> Scrapy
          

          
          </a>

          
            
            
              <div class="version">
                2.3
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">第一步</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/overview.html">Scrapy一目了然</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/install.html">安装指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/tutorial.html">Scrapy 教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/examples.html">实例</a></li>
</ul>
<p class="caption"><span class="caption-text">基本概念</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="commands.html">命令行工具</a></li>
<li class="toctree-l1"><a class="reference internal" href="spiders.html">蜘蛛</a></li>
<li class="toctree-l1"><a class="reference internal" href="selectors.html">选择器</a></li>
<li class="toctree-l1"><a class="reference internal" href="items.html">项目</a></li>
<li class="toctree-l1"><a class="reference internal" href="loaders.html">项目加载器</a></li>
<li class="toctree-l1"><a class="reference internal" href="shell.html">Scrapy shell</a></li>
<li class="toctree-l1"><a class="reference internal" href="item-pipeline.html">项目管道</a></li>
<li class="toctree-l1"><a class="reference internal" href="feed-exports.html">Feed 导出</a></li>
<li class="toctree-l1"><a class="reference internal" href="request-response.html">请求和响应</a></li>
<li class="toctree-l1"><a class="reference internal" href="link-extractors.html">链接提取器</a></li>
<li class="toctree-l1"><a class="reference internal" href="settings.html">设置</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">例外情况</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#built-in-exceptions-reference">内置异常引用</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#closespider">CloseSpider</a></li>
<li class="toctree-l3"><a class="reference internal" href="#dontclosespider">DontCloseSpider</a></li>
<li class="toctree-l3"><a class="reference internal" href="#dropitem">DropItem</a></li>
<li class="toctree-l3"><a class="reference internal" href="#ignorerequest">IgnoreRequest</a></li>
<li class="toctree-l3"><a class="reference internal" href="#notconfigured">NotConfigured</a></li>
<li class="toctree-l3"><a class="reference internal" href="#notsupported">NotSupported</a></li>
<li class="toctree-l3"><a class="reference internal" href="#stopdownload">StopDownload</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<p class="caption"><span class="caption-text">内置服务</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="logging.html">登录</a></li>
<li class="toctree-l1"><a class="reference internal" href="stats.html">统计数据集合</a></li>
<li class="toctree-l1"><a class="reference internal" href="email.html">发送电子邮件</a></li>
<li class="toctree-l1"><a class="reference internal" href="telnetconsole.html">远程登录控制台</a></li>
<li class="toctree-l1"><a class="reference internal" href="webservice.html">Web服务</a></li>
</ul>
<p class="caption"><span class="caption-text">解决具体问题</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">常见问题</a></li>
<li class="toctree-l1"><a class="reference internal" href="debug.html">调试spiders</a></li>
<li class="toctree-l1"><a class="reference internal" href="contracts.html">蜘蛛合约</a></li>
<li class="toctree-l1"><a class="reference internal" href="practices.html">常用做法</a></li>
<li class="toctree-l1"><a class="reference internal" href="broad-crawls.html">宽爬行</a></li>
<li class="toctree-l1"><a class="reference internal" href="developer-tools.html">使用浏览器的开发人员工具进行抓取</a></li>
<li class="toctree-l1"><a class="reference internal" href="dynamic-content.html">选择动态加载的内容</a></li>
<li class="toctree-l1"><a class="reference internal" href="leaks.html">调试内存泄漏</a></li>
<li class="toctree-l1"><a class="reference internal" href="media-pipeline.html">下载和处理文件和图像</a></li>
<li class="toctree-l1"><a class="reference internal" href="deploy.html">部署蜘蛛</a></li>
<li class="toctree-l1"><a class="reference internal" href="autothrottle.html">AutoThrottle 扩展</a></li>
<li class="toctree-l1"><a class="reference internal" href="benchmarking.html">标杆管理</a></li>
<li class="toctree-l1"><a class="reference internal" href="jobs.html">作业：暂停和恢复爬行</a></li>
<li class="toctree-l1"><a class="reference internal" href="coroutines.html">协同程序</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio.html">asyncio</a></li>
</ul>
<p class="caption"><span class="caption-text">扩展Scrapy</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="architecture.html">体系结构概述</a></li>
<li class="toctree-l1"><a class="reference internal" href="downloader-middleware.html">下载器中间件</a></li>
<li class="toctree-l1"><a class="reference internal" href="spider-middleware.html">蜘蛛中间件</a></li>
<li class="toctree-l1"><a class="reference internal" href="extensions.html">扩展</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">核心API</a></li>
<li class="toctree-l1"><a class="reference internal" href="signals.html">信号</a></li>
<li class="toctree-l1"><a class="reference internal" href="exporters.html">条目导出器</a></li>
</ul>
<p class="caption"><span class="caption-text">其余所有</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../news.html">发行说明</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributing.html">为 Scrapy 贡献</a></li>
<li class="toctree-l1"><a class="reference internal" href="../versioning.html">版本控制和API稳定性</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Scrapy</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
        
      <li>例外情况</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="module-scrapy.exceptions">
<span id="exceptions"></span><span id="topics-exceptions"></span><h1>例外情况<a class="headerlink" href="#module-scrapy.exceptions" title="永久链接至标题">¶</a></h1>
<div class="section" id="built-in-exceptions-reference">
<span id="topics-exceptions-ref"></span><h2>内置异常引用<a class="headerlink" href="#built-in-exceptions-reference" title="永久链接至标题">¶</a></h2>
<p>下面是scrapy中包含的所有异常及其用法的列表。</p>
<div class="section" id="closespider">
<h3>CloseSpider<a class="headerlink" href="#closespider" title="永久链接至标题">¶</a></h3>
<dl class="py exception">
<dt id="scrapy.exceptions.CloseSpider">
<em class="property">exception </em><code class="sig-prename descclassname">scrapy.exceptions.</code><code class="sig-name descname">CloseSpider</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">reason</span><span class="o">=</span><span class="default_value">'cancelled'</span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/scrapy/exceptions.html#CloseSpider"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.exceptions.CloseSpider" title="永久链接至目标">¶</a></dt>
<dd><p>可以从蜘蛛回调中引发此异常以请求关闭/停止蜘蛛。支持的参数：</p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-1466963416408457"
     data-ad-slot="8850786025"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<dl class="field-list simple">
<dt class="field-odd">参数</dt>
<dd class="field-odd"><p><strong>reason</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(在 Python v3.9)"><em>str</em></a>) -- 关闭的原因</p>
</dd>
</dl>
</dd></dl>

<p>例如：：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">parse_page</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response</span><span class="p">):</span>
    <span class="k">if</span> <span class="s1">&#39;Bandwidth exceeded&#39;</span> <span class="ow">in</span> <span class="n">response</span><span class="o">.</span><span class="n">body</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">CloseSpider</span><span class="p">(</span><span class="s1">&#39;bandwidth_exceeded&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="dontclosespider">
<h3>DontCloseSpider<a class="headerlink" href="#dontclosespider" title="永久链接至标题">¶</a></h3>
<dl class="py exception">
<dt id="scrapy.exceptions.DontCloseSpider">
<em class="property">exception </em><code class="sig-prename descclassname">scrapy.exceptions.</code><code class="sig-name descname">DontCloseSpider</code><a class="reference internal" href="../_modules/scrapy/exceptions.html#DontCloseSpider"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.exceptions.DontCloseSpider" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>此异常可以在 <a class="reference internal" href="signals.html#std-signal-spider_idle"><code class="xref std std-signal docutils literal notranslate"><span class="pre">spider_idle</span></code></a> 防止蜘蛛关闭的信号处理程序。</p>
</div>
<div class="section" id="dropitem">
<h3>DropItem<a class="headerlink" href="#dropitem" title="永久链接至标题">¶</a></h3>
<dl class="py exception">
<dt id="scrapy.exceptions.DropItem">
<em class="property">exception </em><code class="sig-prename descclassname">scrapy.exceptions.</code><code class="sig-name descname">DropItem</code><a class="reference internal" href="../_modules/scrapy/exceptions.html#DropItem"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.exceptions.DropItem" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>必须由项管道阶段引发的异常才能停止处理项。有关详细信息，请参阅 <a class="reference internal" href="item-pipeline.html#topics-item-pipeline"><span class="std std-ref">项目管道</span></a> .</p>
</div>
<div class="section" id="ignorerequest">
<h3>IgnoreRequest<a class="headerlink" href="#ignorerequest" title="永久链接至标题">¶</a></h3>
<dl class="py exception">
<dt id="scrapy.exceptions.IgnoreRequest">
<em class="property">exception </em><code class="sig-prename descclassname">scrapy.exceptions.</code><code class="sig-name descname">IgnoreRequest</code><a class="reference internal" href="../_modules/scrapy/exceptions.html#IgnoreRequest"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.exceptions.IgnoreRequest" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>调度程序或任何下载器中间件都可以引发此异常，以指示应忽略请求。</p>
</div>
<div class="section" id="notconfigured">
<h3>NotConfigured<a class="headerlink" href="#notconfigured" title="永久链接至标题">¶</a></h3>
<dl class="py exception">
<dt id="scrapy.exceptions.NotConfigured">
<em class="property">exception </em><code class="sig-prename descclassname">scrapy.exceptions.</code><code class="sig-name descname">NotConfigured</code><a class="reference internal" href="../_modules/scrapy/exceptions.html#NotConfigured"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.exceptions.NotConfigured" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>某些组件可能会引发此异常，以指示它们将保持禁用状态。这些组成部分包括：</p>
<blockquote>
<div><ul class="simple">
<li><p>扩展</p></li>
<li><p>项目管道</p></li>
<li><p>下载器中心件</p></li>
<li><p>蜘蛛中心件</p></li>
</ul>
</div></blockquote>
<p>必须在组件的中引发异常 <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法。</p>
</div>
<div class="section" id="notsupported">
<h3>NotSupported<a class="headerlink" href="#notsupported" title="永久链接至标题">¶</a></h3>
<dl class="py exception">
<dt id="scrapy.exceptions.NotSupported">
<em class="property">exception </em><code class="sig-prename descclassname">scrapy.exceptions.</code><code class="sig-name descname">NotSupported</code><a class="reference internal" href="../_modules/scrapy/exceptions.html#NotSupported"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.exceptions.NotSupported" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>引发此异常以指示不支持的功能。</p>
</div>
<div class="section" id="stopdownload">
<h3>StopDownload<a class="headerlink" href="#stopdownload" title="永久链接至标题">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified added">2.2 新版功能.</span></p>
</div>
<dl class="py exception">
<dt id="scrapy.exceptions.StopDownload">
<em class="property">exception </em><code class="sig-prename descclassname">scrapy.exceptions.</code><code class="sig-name descname">StopDownload</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">fail</span><span class="o">=</span><span class="default_value">True</span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/scrapy/exceptions.html#StopDownload"><span class="viewcode-link">[源代码]</span></a><a class="headerlink" href="#scrapy.exceptions.StopDownload" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>从一个 <a class="reference internal" href="signals.html#scrapy.signals.bytes_received" title="scrapy.signals.bytes_received"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes_received</span></code></a> 信号处理程序，指示不应为响应下载更多字节。</p>
<p>这个 <code class="docutils literal notranslate"><span class="pre">fail</span></code> 布尔参数控制将处理结果响应的方法：</p>
<ul class="simple">
<li><p>如果 <code class="docutils literal notranslate"><span class="pre">fail=True</span></code> （默认），调用请求errback。响应对象可用作 <code class="docutils literal notranslate"><span class="pre">response</span></code> 的属性 <code class="docutils literal notranslate"><span class="pre">StopDownload</span></code> 异常，它又存储为 <code class="docutils literal notranslate"><span class="pre">value</span></code> 接收的属性 <a class="reference external" href="https://twistedmatrix.com/documents/current/api/twisted.python.failure.Failure.html" title="(在 Twisted v2.0)"><code class="xref py py-class docutils literal notranslate"><span class="pre">Failure</span></code></a> 对象。这意味着在一个定义为 <code class="docutils literal notranslate"><span class="pre">def</span> <span class="pre">errback(self,</span> <span class="pre">failure)</span></code> ，但可以访问响应 <code class="docutils literal notranslate"><span class="pre">failure.value.response</span></code> .</p></li>
<li><p>如果 <code class="docutils literal notranslate"><span class="pre">fail=False</span></code> ，而是调用请求回调。</p></li>
</ul>
<p>在这两种情况下，响应的主体都可能被截断：主体包含在引发异常之前接收到的所有字节，包括在引发异常的信号处理程序中接收到的字节。另外，响应对象标记为 <code class="docutils literal notranslate"><span class="pre">&quot;download_stopped&quot;</span></code> 在其 <code class="xref py py-attr docutils literal notranslate"><span class="pre">Response.flags</span></code> 属性。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p><code class="docutils literal notranslate"><span class="pre">fail</span></code> 是一个仅限关键字的参数，即升高 <code class="docutils literal notranslate"><span class="pre">StopDownload(False)</span></code> 或 <code class="docutils literal notranslate"><span class="pre">StopDownload(True)</span></code> 将提高 <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#TypeError" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">TypeError</span></code></a> .</p>
</div>
<p>有关 <a class="reference internal" href="signals.html#scrapy.signals.bytes_received" title="scrapy.signals.bytes_received"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes_received</span></code></a> 信号和 <a class="reference internal" href="request-response.html#topics-stop-response-download"><span class="std std-ref">停止下载响应</span></a> 主题以获取更多信息和示例。</p>
</div>
</div>
</div>


           </div>
           
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="logging.html" class="btn btn-neutral float-right" title="登录" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="settings.html" class="btn btn-neutral float-left" title="设置" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        
        &copy; 版权所有 2008–2020, Scrapy developers
      <span class="lastupdated">
        最后更新于 10月 18, 2020.
      </span>

    </p>
  </div>
    
    
    
    Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
  
 
<script type="text/javascript">
!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="3.1.0";
analytics.load("8UDQfnf3cyFSTsM4YANnW5sXmgZVILbA");
analytics.page();
}}();

analytics.ready(function () {
    ga('require', 'linker');
    ga('linker:autoLink', ['scrapinghub.com', 'crawlera.com']);
});
</script>


</body>
</html>